<template>
  <div style="margin-top: -20px">
    <div style="display: flex;margin-bottom: 0px;align-items: center">
      <div>
        <el-button
          type="primary"
          size="mini"
          plain
          @click="fanhui()"
        >
          返回
        </el-button>
      </div>
      <h1 style="margin-left: 15px">查看用户关联数据</h1>
    </div>
    <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="1">资产({{ total }})</el-menu-item>
      <el-menu-item index="2">许可证({{ total1 }})</el-menu-item>
      <el-menu-item index="3">附属品({{ total2 }})</el-menu-item>
      <el-menu-item index="4">消耗品({{ total3 }})</el-menu-item>
    </el-menu>
    <div class="line"></div>
    <!--资产-->
    <div v-if="activeIndex === '1'">
      <el-table v-loading="loading" :data="assetList">
        <el-table-column width="200" label="资产名称" align="left" prop="name">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="设备图片" align="center" prop="image">
          <template slot-scope="scope">
            <img v-if="scope.row.image" :src="scope.row.image" style="width: 50px;height: 50px" />
          </template>
        </el-table-column>
        <el-table-column width="200" label="资产标签" align="left" prop="asset_tag" />
        <el-table-column width="200" label="序列号" align="left" prop="serial" />
        <el-table-column width="200" label="资产型号" align="left" prop="model_name" />
        <el-table-column width="200" label="资产状态" align="center" prop="status">
          <template slot-scope="scope">
            <span v-if="scope.row.status1">{{ scope.row.status1 }}</span>
            <el-tag v-if="scope.row.assigned_to_name" type="success" style="margin-left: 12px">已分配</el-tag>
          </template>
        </el-table-column>
        <el-table-column width="200" label="位置" align="center" prop="location_name" />
        <el-table-column width="200" label="采购价格" align="center" prop="purchase_cost" />
        <el-table-column v-if="false" width="200" label="归还/借出" align="center" prop="assigned_to" fixed="right">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.assigned_to_name"
              plain
              :disabled="scope.row.status!=='deployable'"
              type="primary"
              size="mini"
              @click="changeInOut(0,scope.row)"
            >归还</el-button>
            <el-button
              v-if="!scope.row.assigned_to_name"
              plain
              :disabled="scope.row.status!=='deployable'"
              type="primary"
              size="mini"
              :loading="exportLoading"
              @click="changeInOut(1,scope.row)"
            >借出</el-button>
          </template>
        </el-table-column>
        <el-table-column v-if="false" width="200" label="盘点" align="center" prop="assigned_to" fixed="right">
          <template slot-scope="scope">
            <el-button
              plain
              :disabled="false"
              type="primary"
              size="mini"
              @click="panPoint(scope.row)"
            >盘点</el-button>
          </template>
        </el-table-column>
        <el-table-column width="200" label="操作" align="center" prop="assigned_to" fixed="right">
          <template slot-scope="scope">
            <el-button
              plain
              :disabled="false"
              type="primary"
              size="mini"
              @click="searchAssets(scope.row)"
            >查看</el-button>
          </template>
        </el-table-column>
        <el-table-column width="200" label="借至" align="left" prop="assigned_to_name">
          <template slot-scope="scope">
            <span>{{ scope.row.assigned_to_name?scope.row.assigned_to_name:'--' }}</span>
          </template>
        </el-table-column>
        <el-table-column width="200" label="供应商" align="left" prop="supplier_name" />
        <el-table-column v-if="false" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['admin:assets:assets:put']"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              v-hasPermi="['admin:assets:assets:delete']"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getAssetrecordss"
      />
    </div>
    <!--许可证-->
    <div v-if="activeIndex === '2'&&this.chooseItemTwo">
      <BorrowTooList key="zujian1" :asset-id="this.chooseItemTwo.assigned_to_id+''" asset-type="license" assigned-type="user" @updateNumber="updateNumberXkz"></BorrowTooList>
    </div>
    <!--附属品-->
    <div v-if="activeIndex === '3'&&this.chooseItemTwo">
      <BorrowTooList key="zujian2" :asset-id="this.chooseItemTwo.assigned_to_id+''" asset-type="accessory" assigned-type="user" @updateNumber="updateNumberFsp"></BorrowTooList>
    </div>
    <!--消耗品-->
    <div v-if="activeIndex === '4'&&this.chooseItemTwo">
      <BorrowTooList key="zujian3" :asset-id="this.chooseItemTwo.assigned_to_id+''" asset-type="consumable" assigned-type="user" @updateNumber="updateNumberXhp"></BorrowTooList>
    </div>
  </div>
</template>

<script>
import FileUpload from "@/components/FileUpload/index";
import file from "@/views/components/file";
import { listAsset } from "@/api/vadmin/assets/assets";
import BorrowTooList from "@/views/components/BorrowTooList1";
import { assignedInfo } from "@/api/vadmin/basic/base";

export default {
  name: "AssetsDetail",
  components: { FileUpload, file, BorrowTooList },
  props: {
    topId: {
      type: Number,
      default: 0
    },
    chooseItem: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      // 资产
      assetList: [],
      // 附属品
      fushupingList: [],
      // 许可证
      accessorieslist: [],
      // 消耗品
      consumablerList: [],
      categoryTypeName: "asset",
      chooseItemTwo: null,
      activeIndex: "1",
      title1: "",
      open1: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total1: 0,
      total2: 0,
      total3: 0,
      // vadmin/assets/表格数据
      modelsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        kit: null,
        tag: "model"
      },
      queryParams1: {
        pageNum: 1,
        pageSize: 50
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 50
      },
      queryParams3: {
        pageNum: 1,
        pageSize: 50
      },
      // 表单参数
      form: {
        kit: null,
        tag: "model",
        quantity: 2,
        id: null
      },
      compenentList: [],
      compenentSize: 0,
      licenseslist: [],
      whtotal: 0,
      fileTotal: 0,
      assetStatus: []
    };
  },
  mounted() {
    this.chooseItemTwo = this.chooseItem;
    this.getDicts("asset_status").then(response => {
      this.assetStatus = response.data;
    });
    this.getAssetrecordss();
    assignedInfo({ asset_type: "license", assigned_to_id: this.chooseItemTwo.assigned_to_id, assigned_type: "user" }).then(res => {
      this.total1 = res.data.count;
    });
    assignedInfo({ asset_type: "accessory", assigned_to_id: this.chooseItemTwo.assigned_to_id, assigned_type: "user" }).then(res => {
      this.total2 = res.data.count;
    });
    assignedInfo({ asset_type: "consumable", assigned_to_id: this.chooseItemTwo.assigned_to_id, assigned_type: "user" }).then(res => {
      this.total3 = res.data.count;
    });
  },
  methods: {
    updateNumberXkz(val) {
      this.total1 = val;
    },
    updateNumberFsp(val) {
      this.total2 = val;
    },
    updateNumberXhp(val) {
      this.total3 = val;
    },
    searchAssets(val) {
      if (this.$route.path !== "/assets/assets") {
        this.$router.push({ path: "/assets/assets", query: { searchKey: val.asset_tag }});
      }
    },
    // 获取资产表
    getAssetrecordss() {
      this.loading = true;
      listAsset({ assigned_type: "user", assigned_to_id: this.chooseItemTwo.assigned_to_id, pageNum: this.queryParams.pageNum, pageSize: this.queryParams.pageSize }).then(res => {
        this.assetList = res.data.results;
        this.assetList.filter(res => {
          res.status1 = this.selectDictLabel(this.assetStatus, res.status);
        });
        this.loading = false;
        this.total = res.data.count;
      });
    },
    handleSelect(key, keyPath) {
      this.activeIndex = key + "";
      if (this.activeIndex === "1") {
        this.getAssetrecordss();
      } else if (this.activeIndex === "2") {
        assignedInfo({ asset_type: "license", assigned_to_id: this.chooseItemTwo.assigned_to_id, assigned_type: "user" }).then(res => {
          this.total1 = res.data.count;
        });
      } else if (this.activeIndex === "3") {
        assignedInfo({ asset_type: "accessory", assigned_to_id: this.chooseItemTwo.assigned_to_id, assigned_type: "user" }).then(res => {
          this.total2 = res.data.count;
        });
      } else if (this.activeIndex === "4") {
        assignedInfo({ asset_type: "consumable", assigned_to_id: this.chooseItemTwo.assigned_to_id, assigned_type: "user" }).then(res => {
          this.total3 = res.data.count;
        });
      }
      this.$forceUpdate();
    },
    fanhui() {
      this.$emit("fanhui");
    }
  }
};
</script>
<style>
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  margin-top: 15px;
}
</style>
